package com.meal.dataway.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meal.common.model.GenericPage;
import com.meal.common.model.Result;
import com.meal.dataway.service.base.DictService;
import com.meal.entity.annotation.ValidParam;
import com.meal.entity.base.BaseController;
import com.meal.entity.entity.base.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author yzh
 * @since 2019-12-11
 */
@Api(tags = {"字典表"})
@Log4j
@RestController
@RequestMapping(value = "/dict", produces = {MediaType.APPLICATION_JSON_VALUE})
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @RequestMapping("/getDict")
    @ApiOperation(value = "获取字典表信息", notes = "获取字典表信息", httpMethod = "GET")
    public Result getDict(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize){
        Page<Dict> page = new Page<>(pageNo, pageSize);
        IPage<Dict> pageResult = dictService.page(page, Wrappers.<Dict>lambdaQuery().orderByDesc(Dict::getCreateTime));
        return createSuccessResult(pageResult);
    }


    @GetMapping("/getDictGen")
    @ApiOperation(value = "获取字典表信息", notes = "获取字典表信息", httpMethod = "GET")
    public Result getDictGen(@ModelAttribute GenericPage genericPage){
        Page<Dict> page = new Page<>(genericPage.getPageNo(), genericPage.getPageSize());
        IPage<Dict> pageResult = dictService.page(page, Wrappers.<Dict>lambdaQuery().orderByDesc(Dict::getCreateTime));
        return createSuccessResult(pageResult);
    }

    @RequestMapping("/saveDictPara")
    @ApiOperation(value = "获取字典表信息", notes = "获取字典表信息", httpMethod = "POST")
    public Result saveDictPara(String name, Model model){
        Map<String, String> modelMap = (Map<String, String>) model.getAttribute("modelAttr");
        log.info(modelMap);
        return SUCCESS;
    }

    @ValidParam
    @RequestMapping("/saveDict")
    @ApiOperation(value = "获取字典表信息", notes = "获取字典表信息", httpMethod = "POST")
    public Result saveDict(@Valid Dict dict, BindingResult bindingResult, Model model){
        Map<String, String> modelMap = (Map<String, String>) model.getAttribute("modelAttr");
        log.info(modelMap);
        return SUCCESS;
    }
}

